<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械调拨</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>

<body>
<style type="text/css">
	*{
		font-size:12px;
	}
	#QiXieAllocationContent{
	}
	#QiXieAllocationInfo{
		width:100%;
		overflow: hidden;
	}
	li{
		float: left;
		list-style: none;
	}
</style>
<script type="text/javascript">
	var editIndex = undefined;
	//结束正在编辑的行
	function endEditing() {
		if (editIndex == undefined) {
			return true
		}
		if ($('#qxAllocationtable').datagrid('validateRow', editIndex)) {
			$('#qxAllocationtable').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	//事件：单击单元格的事件
	function onClickCell(index, field) {
		if (editIndex != index) {
			if (endEditing()) {
				$('#qxAllocationtable').datagrid('selectRow', index).datagrid('beginEdit',
						index);
				var ed = $('#qxAllocationtable').datagrid('getEditor', {
					index : index,
					field : field
				});
				if (ed) {
					($(ed.target).data('textbox') ? $(ed.target).textbox(
							'textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function() {
					$('#qxAllocationtable').datagrid('selectRow', editIndex);
				}, 0);
			}
		}
	}
	
	//结束编辑，同步数据
	function onEndEdit(index, row) {
		var ed = $(this).datagrid('getEditor', {
			index : index,
			field : 'dbunit'
		});
		if(ed){
			row.cgUnit = $(ed.target).combobox('getText');
		}else{
			row.cgUnit = null;
		}
	}
	
	//删除行
	function removeit() {
		var datas=$('#qxAllocationtable').datagrid('getSelections');
		for(var i=0;i<datas.length;i++){
	        var data=datas[i];
	        var index=$('#qxAllocationtable').datagrid('getRowIndex',data);
	        editIndex=index;
	        $('#qxAllocationtable').datagrid('deleteRow',index);
	    }
	}
	
	//应用
	function accept() {
		if (endEditing()) {
			//$('#qxAllocationtable').datagrid('acceptChanges');
			endEditing();
		}
	}
	
	//撤消
	function reject() {
		$('#qxAllocationtable').datagrid('rejectChanges');
		editIndex = undefined;
	}
	
	//获取数据
	function getChanges() {
		var rows = $('#qxAllocationtable').datagrid('getChanges');
		alert(rows.length + ' rows are changed!');
	}

$(function(){
	//赋值
	$('#warehouseCpName').combogrid({
		onChange : function(newValue , oldValue){
			var kcName=$('#warehouseCpName').combogrid('getText');
			$.getJSON("selectHisInventoryByName", {"kcName": kcName}, function(data){
				$('#qxdbcksl').numberbox('setValue',data.kcNumber);
				$('#qxdbsl').numberspinner({max:data.kcNumber});
				$.getJSON("selectHisWarehouseByckNo", {"ckNo": data.ckNo}, function(data){
					$('#qxdbck').combogrid('setValue',data.ckName);
				});
				$.getJSON("selectHisPurchaseByName", {"cpName": kcName}, function(data){
					$('#qxdbCpPh').textbox('setValue',data.cgBatchnumber);
				});
			});
			$('#qxdbCpPh').textbox('clear');
			$('#qxdbcksl').numberbox('clear');
			$('#qxdbck').combogrid('clear');
			$('#qxdbckto').combogrid('clear');
		}
	});
	
	//数字微调
	$('#qxdbsl').numberspinner({
		onChange: function (newValue, oldValue) {
			var qxdbcksl=$('#qxdbcksl').numberbox('getValue');
			if(qxdbcksl==''){
				$.messager.alert('温馨提示', '请先选择您需要调拨的产品名称!','info');
				$('#qxdbsl').numberspinner('setValue',0);
				$('#qxdbcksl').numberbox('setValue','');
			}else{
				var jcsl=$('#qxdbcksl').numberbox('getValue');
				var o=parseInt(jcsl)+parseInt(oldValue)-parseInt(newValue);
				$('#qxdbcksl').numberbox('setValue',o);
			}
		}
	});
	
	//加入临时表
	$('#add-lsdbTable').linkbutton({
		onClick : function(){
			var warehouseCpName=$('#warehouseCpName').combogrid('getText');
			var qxdbcksl=$('#qxdbcksl').numberbox('getText');
			var qxdbsl=$('#qxdbsl').numberspinner('getText');
			var qxdbCpPh=$('#qxdbCpPh').textbox('getText');
			var qxdbck=$('#qxdbck').combogrid('getText');
			var qxdbckto=$('#qxdbckto').combogrid('getText');
			var qxdbDate=$('#qxdbDate').datebox('getText');
			var qxdbAdmin=$('#qxdbAdmin').combogrid('getText');
			$.getJSON("selectHisPurchaseByName", {"cpName": warehouseCpName}, function(data){
				var unit=data.cgUnit;
				var id=data.cgNo;
				var rows=$('#qxAllocationtable').datagrid('getRows');
				var ii=0;
				for(var i=0;i<rows.length;i++){
					var name=rows[i]['hisAllocationVos.dbCpName'];
					if(name==warehouseCpName){
						ii++;
					}
				}
				if(qxdbDate=='' || qxdbAdmin=='' || warehouseCpName=='' || qxdbckto=='' || parseInt(qxdbsl)<1 ){
					$.messager.alert("温馨提示", "请把数据填充完整!",'info');
				}else if(ii>0){
					$.messager.alert("提示", "产品已存在临时调拨中，请勿重复添加!");
					$('#qxdbInfo').form("clear");
				}else{
					$('#qxAllocationtable').datagrid('appendRow', {
						'hisAllocationVos.dbCpId' : id,
						'hisAllocationVos.dbCpName' : warehouseCpName,
						'hisAllocationVos.kcNumber' : qxdbcksl,
						'hisAllocationVos.dbNumber' : qxdbsl,
						'hisAllocationVos.dbunit' : unit,
						'hisAllocationVos.ckName' : qxdbck,
						'hisAllocationVos.ckNameto' : qxdbckto,
						'hisAllocationVos.dbCpPh' : qxdbCpPh
					});
					$('#qxdbInfo').form("clear");
				}
			});
		}
	});
	
	//调拨
	$('#submit-addAllocation').linkbutton({
		onClick : function(){
			//将datagrid中的数据行保存到隐式表单域中
			//1、获取新增的数据行
			var rows = $('#qxAllocationtable').datagrid('getChanges');
			//为每一行生成对应的隐藏表单域
			for(var i = 0; i < rows.length; i ++){
				generateHidden(rows[i], "dbCpId", i);
				generateHidden(rows[i], "dbCpName", i);
				generateHidden(rows[i], "kcNumber", i);
				generateHidden(rows[i], "dbNumber", i);
				generateHidden(rows[i], "dbunit", i);
				generateHidden(rows[i], "ckName", i);
				generateHidden(rows[i], "ckNameto", i);
				generateHidden(rows[i], "dbCpPh", i);
			}
			//提交表单
			if(rows.length != 0){
				$('#ff_QixieAllocation').form('submit', {
					success: function(v){
						var json = eval("(" + v + ")");
						//alert(json);
						if(json=='操作成功'){
							$(":hidden[x='y']").remove();
							$('#qxAllocationtable').datagrid('loadData', { total: 0, rows: [] });
							$.messager.alert('温馨提示', "调拨"+json);
							$('#ff_QixieAllocation').form("clear");
							$("#win-QiXieAllocation").window("close");
						}else{
							$.messager.alert('温馨提示', json,'info');
						}
					}
				});
			}
		}
	});
	
	function generateHidden(row, name, i) {
		var ff_QixieAllocation = $("#ff_QixieAllocation");
		$("<input type='hidden' x='y'>").attr("name",
				"hisAllocationVos[" + i + '].'  + name).val(
				row['hisAllocationVos.'+name]).appendTo(ff_QixieAllocation);
	}
});
</script>

	<!-- 器械调拨 -->
	<!-- <div id="win-QiXieAllocation" class="easyui-window" title="器械调拨" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-thpAllocation'"
		style="width: 650px; height: 350px;"> -->
		<form id="ff_QixieAllocation" action="QiXieHisAllocation" method="post">
			<div id="QiXieAllocationContent">
				<div id="QiXieAllocationInfo" style="background-color: rgb(236,243,255)">
					<ul>
						<li style="margin:5px 0px 10px 0px;">调拨日期&nbsp;<input id="qxdbDate" class="easyui-datebox" data-options="width: 100"></li>
						<li style="margin:5px 0px 10px 20px;">仓管员&nbsp;<select id="qxdbAdmin" class="easyui-combogrid" data-options="width: 100,
							url:'findAllEmployeeWarehouse',
							idField:'ygNo',
							panelHeight:'auto',
							panelWidth:225,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></select></li>
						<li style="margin:5px 0px 10px 190px;"><a id="submit-addAllocation" class="easyui-linkbutton">&nbsp;&nbsp;调拨处理&nbsp;&nbsp;</a></li>
					</ul>
				</div>
				<div style="border-top:1px solid #95B8e7;width:100%;height:250px;overflow: hidden;">
					<div>
			        	<div id="qxdbInfo" style="overflow: hidden;">
			        		<ul>
								<li style="margin:10px 30px 10px 0px;">器械&nbsp;<select id="warehouseCpName" class="easyui-combogrid" data-options="width: 188,
									url:'findAllHisInventory',
									idField:'kcNo',
									panelHeight:'auto',
									textField:'kcName',
									columns:[[{field:'kcName',title:'仓库器械名称',width:'180px'}
									]]"></select></li>
								<li style="margin:10px 30px 10px 0px;">库存数量&nbsp;<input id="qxdbcksl" class="easyui-numberbox" data-options="width: 100" readonly="readonly"></li>
								<li style="margin:10px 0px 10px 0px;">调拨数量&nbsp;<input id="qxdbsl" value="0" class="easyui-numberspinner" data-options="min: 0,width: 100"></li>
								<li style="margin:0px 45px 10px 0px;">批号&nbsp;<input id="qxdbCpPh" class="easyui-textbox" data-options="width: 100" readonly="readonly"></li>
								<li style="margin:0px 5px 10px 0px;">调拨仓库&nbsp;<input id="qxdbck" class="easyui-combogrid" data-options="width: 124" readonly="readonly"></li>
								<li style="margin:0px 29px 10px 0px;">至&nbsp;<input id="qxdbckto" class="easyui-combogrid" data-options="width: 100,
									url:'selectAllHisWarehouse',
									idField:'ckNo',
									panelHeight:'auto',
									textField:'ckName',
									columns:[[
							            {field:'ckName',title:'仓库名称',width:'96px'}
									]]"></li>
								<li style="margin:0px 0px 10px 0px;"><a id="add-lsdbTable" class="easyui-linkbutton" style="width:80px;">加入</a></li>
							</ul>
			        	</div>
			        	<div style="border:0px red solid;height:160px;">
							<table id="qxAllocationtable" class="easyui-datagrid"
								data-options="
									fitColumns:true,
									fit:true,
									singleSelect: true,
									rownumbers: true,
									toolbar: '#qxdbtb',
									border:false
									">
								<thead>
									<tr>
										<th data-options="field:'hisAllocationVos.dbCpId',width:'15%'">器械编号</th>
										<th data-options="field:'hisAllocationVos.dbCpName',width:'15%'">器械名称</th>
										<th data-options="field:'hisAllocationVos.kcNumber',width:'10%'">库存数量</th>
										<th data-options="field:'hisAllocationVos.dbNumber',width:'10%'">调拨数量</th>
										<th data-options="field:'hisAllocationVos.dbunit',width:'10%'">单位</th>
										<th data-options="field:'hisAllocationVos.ckName',width:'15%'">调拨仓库</th>
										<th data-options="field:'hisAllocationVos.ckNameto',width:'15%'">至仓库</th>
										<th data-options="field:'hisAllocationVos.dbCpPh',width:'12%'">批号</th>
									</tr>
								</thead>
							</table>
							<div id="qxdbtb" style="height: auto;padding-left: 30px;">
								<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-add',plain:true">添加一行</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-remove',plain:true"
									onclick="removeit()">移除一行</a> <a href="javascript:void(0)"
									class="easyui-linkbutton"
									data-options="iconCls:'icon-ok',plain:true">应用</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
							</div>
			        	</div>
			        </div>
			    </div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>